package cece.service.impl;



import cece.entity.Read;
import cece.entity.ReadResp;
import cece.mapper.ReadMapper;
import cece.mapper.Repository.ReadRepository;
import cece.service.ReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class ReadServieImpl implements ReadService {

    @Autowired
    ReadRepository readRepository;
    @Resource
    ReadMapper readMapper;

    @Override
    public ReadResp findAll(Integer pageSize, Integer pageNum) {




        //声明pageAble
        PageRequest pageRequest = new PageRequest(pageNum-1,pageSize);
        //jpa分页
        Page<Read> all = readRepository.findAll(pageRequest);
        List<Read> content = all.getContent();
        //获取总数
        long totalElements = all.getTotalElements();

        ReadResp readResp = new ReadResp();
        readResp.setTotal(totalElements);
        readResp.setReadList(content);
        return readResp;

    }

    @Override
    public Read findById(Integer bid) {
        Optional<Read> byId = readRepository.findById(bid);
        if (byId.isPresent()){
            return byId.get();
        }
        return null;
    }

    @Override
    public List<Read> findByNameLike(String rname) {
        return readMapper.findByNameLike(rname);
    }



}
